package cn.ac.istic.lkt.atv;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

public class FrmTree2Tree extends FrmAbstract {
	private static final long serialVersionUID = 8095225474099077834L;
	TreePanel2Tree treePanelDouble = null;
	String direction="fe";

	public FrmTree2Tree() {
		super();
		treePanelDouble = new TreePanel2Tree();
		
		menuItemLoad.addActionListener(new myListener(this));
		menuItemExportSen.addActionListener(new myListener(this));
		menuItemExportImg.addActionListener(new myListener(this));
		menuItemExit.addActionListener(new myListener(this));
		
		this.setVisible(true);

	}


	public void readAndParse(String f, String e, String a,String d){
		setDirection(d);
		ffname = f;
		efname = e;
		afname = a;
		File ff = new File(f);
		File ef = new File(e);
		File af = new File(a);
		
		stlistf = SentenceTree.parseFile(ff);
		stliste = SentenceTree.parseFile(ef);
		stlista = new ArrayList<String>();
		readAlignment(af);
		treePanelDouble.setDirection(this.direction);
		
		DefaultListModel model = getSenPanel().getModel();
		model.removeAllElements();
		for (int i = 0; i < stlistf.size(); i++) {
			model.addElement(stlistf.get(i).getSents());
		}
		senPanel.getList().setSelectedIndex(0);		

	}
	public void readAlignment(File f){
		InputStream in = null;
		try {
			in = new FileInputStream(f);
		    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
		    String line = null;
		    while ((line = reader.readLine()) != null) {
		    	//System.out.println(line);
		    	if(line.trim().length()==0){
		    		continue;
		    	}		        
		        stlista.add(line.trim());
		    }		    
		} catch (IOException x) {
		    System.err.println(x);		    
		} 
	}
	
	public void drawTree(int id) {
		ParseTree pt1 = stlistf.get(id).getTrees();
		ParseTree pt2 = stliste.get(id).getTrees();
		scrollPane.setViewportView(treePanelDouble);
		treePanelDouble.init(pt1, pt2,stlista.get(id),this.direction);
	}

	
	public void exportImage() {
		if (stlistf == null || stlistf.size() == 0 ||
				stliste == null || stliste.size() == 0 ||
				stlista == null || stlista.size() == 0 ||
				stlistf.size() != stliste.size() || stlistf.size() != stlista.size()) {
			JOptionPane.showMessageDialog(this,
					"Tree File Load error!", "Error!",
					JOptionPane.WARNING_MESSAGE);
			System.err.println("Trees are not loaded yet!");
			return;
		}
		for (int i = 0; i < stlistf.size(); i++) {
			ParseTree pt = stlistf.get(i).getTrees();
			BufferedImage img ;
			
			TreePanel2Tree tp = new TreePanel2Tree();
			ParseTree pt2 = stliste.get(i).getTrees();
			tp.init(pt,pt2,stlista.get(i),this.direction);
			img = tp.paintImage();			
			
			try {
				File outputfile = new File("C:\\tmp\\img" + i + ".png");
				ImageIO.write(img, "png", outputfile);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}
	
	public void setDirection(String s){
		if ( !s.equals("ef") && !s.equals("fe") ){
			System.err.println("Direction can only be \'fe\' or \'ef\'.");
			return;
		}
		this.direction = s;
	}
	
	class myListener implements ActionListener {
		FrmTree2Tree container;

		public myListener(FrmTree2Tree app) {
			container = app;
		}

		public void actionPerformed(ActionEvent e) {
			// System.out.print(e.getSource());
			if (e.getSource().equals(menuItemExit)) {
				performExit();
			}  else if (e.getSource().equals(menuItemLoad)) {
				fdlg = new FileDlg(FrmTree2Tree.this);
				boolean load = fdlg.showDialog();
				if (load) {
					container.readFiles();
					container.readAndParse(container.getFfname(),container.getEfname(),container.getAfname(),"fe");					

					// container.drawTree(0);
				}
			} else if (e.getSource().equals(menuItemExportSen)) {
				exportSentence(container.stlistf);
			} else if (e.getSource().equals(menuItemExportImg)) {
				exportImage();
			} else {
				;
			}
		}
	}
	
	public static void main(String[] args) {
		FrmTree2Tree t2t = new FrmTree2Tree();
		
		
		if (args.length < 3){
			//t2t.setDirection("fe");
			//t2t.readAndParse("example/cttb.tree.chi","example/cttb.tree.eng","example/cttb.align");			
			
			t2t.readAndParse("example/cttb.tree.eng","example/cttb.tree.chi","example/cttb.align","ef");
		}
		//t2t.drawTree(0);
	}
}
